//
//  ForgetpwdVC.m
//  SchoolCircle
//
//  Created by 战立军 on 16/5/27.
//  Copyright © 2016年 战立军. All rights reserved.
//

#import "ForgetpwdVC.h"
#import "TPKeyboardAvoidingScrollView.h"

#define WH 30

@interface ForgetpwdVC ()
{
     TPKeyboardAvoidingScrollView *_scrollView;
    
    UITextField *_phnum;
    UITextField *_code;
    UITextField *_oldpwd;
    UITextField *_newpwd;
    UITextField *_newpwdrep;
    
    UIButton *_changeBtn;
    
    NSTimer *_tiemr;
    UIButton *_button_getyzm;
    int _num;

}
@end

@implementation ForgetpwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _scrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIUTils getWindowWidth], [UIUTils getWindowHeight])];
    _scrollView.backgroundColor = [UIColor whiteColor];
    // 设置内容大小
    _scrollView.contentSize = CGSizeMake([UIUTils getWindowWidth], [UIUTils getWindowHeight]);
    // 提示用户,Indicators flash
    [_scrollView flashScrollIndicators];
    
    [self.view addSubview:_scrollView];
    
    [self addContentView];
    
    [self addBackView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 添加视图

- (void)addContentView
{
    _newpwd = [[UITextField alloc] initWithFrame:CGRectMake(WH, self.view.center.y, [UIUTils getWindowWidth]-WH*2, 40)];
    _newpwd.borderStyle = UITextBorderStyleRoundedRect;
    _newpwd.placeholder = @"新密码";
    _newpwd.backgroundColor = Color(246, 246, 246);
    [_scrollView addSubview:_newpwd];
    
    _oldpwd = [[UITextField alloc] initWithFrame:CGRectMake(WH, CGRectGetMaxY(_newpwd.frame)-40-20-40, [UIUTils getWindowWidth]-WH*2, 40)];
    _oldpwd.borderStyle = UITextBorderStyleRoundedRect;
    _oldpwd.placeholder = @"原密码";
    _oldpwd.backgroundColor = Color(246, 246, 246);
    [_scrollView addSubview:_oldpwd];
    
    _code = [[UITextField alloc] initWithFrame:CGRectMake(WH, CGRectGetMaxY(_oldpwd.frame)-40-20-40, WH*3.5, 40)];
    _code.borderStyle = UITextBorderStyleRoundedRect;
    _code.placeholder = @"验证码";
    _code.backgroundColor = Color(246, 246, 246);
    [_scrollView addSubview:_code];
    
    
    _button_getyzm = [UIButton buttonWithType:UIButtonTypeCustom];
    _button_getyzm.frame= CGRectMake(20+CGRectGetMaxX(_code.frame), CGRectGetMaxY(_oldpwd.frame)-40-20-40, WH*4.5, 40);
    _button_getyzm.tag=222;
    [_button_getyzm setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    [_button_getyzm setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    _button_getyzm.layer.cornerRadius = 5;
    _button_getyzm.titleLabel.font = [UIFont systemFontOfSize:12];
    _button_getyzm.backgroundColor= Color(246, 246, 246);
    [_button_getyzm addTarget:self action:@selector(getyzm) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:_button_getyzm];

    
    
    _phnum = [[UITextField alloc] initWithFrame:CGRectMake(WH, CGRectGetMaxY(_code.frame)-40-20-40, [UIUTils getWindowWidth]-WH*2, 40)];
    _phnum.borderStyle = UITextBorderStyleRoundedRect;
    _phnum.placeholder = @"手机号";
    _phnum.backgroundColor = Color(246, 246, 246);
    [_scrollView addSubview:_phnum];
    
    _newpwdrep = [[UITextField alloc] initWithFrame:CGRectMake(WH, CGRectGetMaxY(_newpwd.frame)+20, [UIUTils getWindowWidth]-WH*2, 40)];
    _newpwdrep.borderStyle = UITextBorderStyleRoundedRect;
    _newpwdrep.placeholder = @"确认密码";
    _newpwdrep.backgroundColor = Color(246, 246, 246);
    [_scrollView addSubview:_newpwdrep];
    
    
    _changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _changeBtn.frame = CGRectMake(WH, CGRectGetMaxY(_newpwdrep.frame)+20, [UIUTils getWindowWidth]-WH*2, 50);
    [_changeBtn setTitle:@"修改" forState:UIControlStateNormal];
    _changeBtn.layer.cornerRadius = 10;
    [_changeBtn setBackgroundColor:[UIColor redColor]];
    [_scrollView addSubview:_changeBtn];
    
}


#pragma mark 返回

- (void)addBackView
{
    UIImageView * backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIUTils getWindowWidth], 64)];
    backView.image = [UIImage imageNamed:@"uitabr"];
    UIButton *_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _backBtn.frame = CGRectMake(5, 20, 40, 40);
    [_backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [_backBtn addTarget:self action:@selector(backButtonPress) forControlEvents:UIControlEventTouchUpInside];
    backView.userInteractionEnabled = YES;
    [backView addSubview:_backBtn];
    [self.view addSubview:backView];
    
}

- (void)backButtonPress
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma Mark 短信验证

-(void)wad{
    [_tiemr invalidate];
    _tiemr= [NSTimer  scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeButtonName) userInfo:nil repeats:YES];
    _button_getyzm.enabled = NO;
    [_button_getyzm setTitle:@"60秒后重新获取" forState:UIControlStateNormal];
    [_button_getyzm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _num = 60;
}

- (void)changeButtonName{
    if (_num >1) {
        _button_getyzm.enabled = NO;
        _num--;
        NSString * string = [NSString stringWithFormat:@"%d秒后重新获取",_num];
        [_button_getyzm setTitle:string forState:UIControlStateNormal ] ;
    }
    else
    {
        [_tiemr invalidate];
        [_button_getyzm setTitle:@"重新获取密码" forState:UIControlStateNormal];
        _button_getyzm.enabled = YES;
    }
}

-(void)getyzm
{
    
    [_tiemr invalidate];
    [self wad];
}



@end
